package com.nexus.all.controller;

import com.nexus.all.es.model.EsNote;
import com.nexus.all.es.repository.EsNoteRepository;
import com.nexus.all.mongodb.model.MongoNoteHistory;
import com.nexus.all.mongodb.repository.NoteHistoryRepository;
import com.nexus.all.service.UserService;
import com.nexus.bean.annotation.PassLogin;
import com.nexus.bean.result.Result;
import com.nexus.redis.utils.RedisUtil;
import com.nexus.web.controller.CommonController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Slf4j
@RequestMapping("test")
@RestController
@AllArgsConstructor
@Api(tags = "測試管理接口")
public class TestController extends CommonController {

    private final UserService userService;

    private final NoteHistoryRepository noteHistoryRepository;
    private final MongoTemplate mongoTemplate;
    private final EsNoteRepository esNoteRepository;

    @ApiOperation(value = "testProject", notes = "testProject")
    @GetMapping("testProject")
    @PassLogin
    public Result<Object> testProject() {
        return Result.newSuccess("SUCCESS");
    }

    @ApiOperation(value = "testRedis", notes = "testRedis")
    @GetMapping("testRedis")
    @PassLogin
    public Result<Object> testRedis() {
        RedisUtil.set("test", "SUCCESS", TimeUnit.HOURS, 1L);
        Object res = RedisUtil.get("test");
        return Result.newSuccess(res);
    }

    @ApiOperation(value = "testMongodb", notes = "testMongodb")
    @GetMapping("testMongodb")
    @PassLogin
    public Result<Object> testMongodb() {
        List<MongoNoteHistory> list = noteHistoryRepository.findAll();
        return Result.newSuccess(list);
    }

    @ApiOperation(value = "testEs", notes = "testEs")
    @GetMapping("testEs")
    @PassLogin
    public Result<Object> es() {
        Iterable<EsNote> esNotes = esNoteRepository.findAll();
        return Result.newSuccess(esNotes);
    }
}
